package com.kt.uokos.yimo.wx.controller;

import com.kt.uokos.yimo.anno.validator.Order;
import com.kt.uokos.yimo.anno.validator.Sort;
import com.kt.uokos.yimo.dao.Cart;
import com.kt.uokos.yimo.dao.Coupon;
import com.kt.uokos.yimo.dao.CouponUser;
import com.kt.uokos.yimo.service.CouponService;
import com.kt.uokos.yimo.service.CouponUserService;
import com.kt.uokos.yimo.util.JacksonUtil;
import com.kt.uokos.yimo.util.ResponseUtil;
import com.kt.uokos.yimo.vo.CouponVo;
import com.kt.uokos.yimo.wx.annocation.LoginUser;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: TODO
 * @className: WxCouponController.java
 * @author: Kang Hao
 * @date: 2022/4/26 14:53
 **/

@RestController
@RequestMapping("/wx/coupon/")

public class WxCouponController {

    @Resource
    private CouponService couponService;

    @Resource
    private CouponUserService couponUserService;


    @GetMapping("/mylist")
    public Object list(@LoginUser Integer userId,
                        Short status,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order){

        if (userId == null){
            return ResponseUtil.unlogin();
        }

        List<CouponUser> couponUserList = couponUserService.queryList(userId, null, status, page, limit, sort, order);
        ArrayList<CouponVo> couponVoList = new ArrayList<>(couponUserList.size());

        for(CouponUser couponUser : couponUserList){
            Integer couponId = couponUser.getCouponId();
            Coupon coupon = couponService.queryById(couponId);
            CouponVo couponVo = new CouponVo();
            couponVo.setId(couponUser.getId());
            couponVo.setCid(coupon.getId());
            couponVo.setName(coupon.getName());
            couponVo.setDesc(coupon.getDesc());
            couponVo.setTag(coupon.getTag());
            couponVo.setMin(coupon.getMin());
            couponVo.setDiscount(coupon.getDiscount());
            couponVo.setStartTime(couponUser.getStartTime());
            couponVo.setEndTime(couponUser.getEndTime());
            couponVoList.add(couponVo);
        }
        return ResponseUtil.okList(couponVoList,couponUserList);
    }

    @PostMapping("receive")
    public Object receive(@LoginUser Integer userId,@RequestBody String body){
        if (userId == null){
            return  ResponseUtil.unlogin();
        }

        Integer couponId = JacksonUtil.parseInteger(body, "couponId");
        if (couponId == null){
            return ResponseUtil.errs();
        }

        Coupon coupon = couponService.queryById(couponId);
        if(coupon == null){
            return ResponseUtil.errs();
        }

        //判断优惠劵是否领完
        Integer countCoupon = couponService.countCoupon(couponId);
        if ( coupon.getTotal() !=0 && countCoupon>=coupon.getTotal()){
            return ResponseUtil.fail(404,"优惠劵已领取完");
        }

        //判断用户是否已达到此优惠劵领取最大数量
        Integer userCoupon = couponUserService.countUserAndCoupon(userId, couponId);
        Integer limit = coupon.getLimit().intValue();
        if (limit !=0 && userCoupon >= limit){
            return ResponseUtil.fail(404,"已领取此优惠券数量已达最大");
        }


        Short type = coupon.getType();
        if (type == 1){
            return ResponseUtil.fail(404,"新用户会自动发送");
        }
        else if (type == 2){
            return ResponseUtil.fail(404,"此优惠劵只能由优惠劵码兑换");
        }
        else if (type != 0){
            return ResponseUtil.fail(404,"优惠劵类型不支持");
        }


        Short status = coupon.getStatus();
        if (status == 1){
            return ResponseUtil.fail(404,"优惠劵已领完");
        }else if (status == 2){
            return ResponseUtil.fail(404,"优惠劵已过期");
        }

        CouponUser couponUser = new CouponUser();
        couponUser.setCouponId(couponId);
        couponUser.setUserId(userId);
        if (coupon.getTimeType() ==1){
            couponUser.setStartTime(coupon.getStartTime());
            couponUser.setEndTime(coupon.getEndTime());
        }else {
            couponUser.setStartTime(LocalDateTime.now());
            couponUser.setEndTime(LocalDateTime.now().plusDays(coupon.getDays()));
        }

        couponUserService.add(couponUser);
        return ResponseUtil.ok();
    }



}
